Imports System.Collections.Specialized
Imports System.Windows.Forms
Imports Microsoft.Win32

Public Class SystemUtil
  'Some allround utilities

  'Sealed class!
  Private Sub New()

  End Sub

  Public Shared Sub Busy(ByVal objCtrl As Control, ByVal bBusy As Boolean)
    Static objCursor As Cursor
    If bBusy Then
      objCursor = objCtrl.Cursor
      objCtrl.Cursor = System.Windows.Forms.Cursors.WaitCursor
    Else
      objCtrl.Cursor = objCursor
    End If
  End Sub

    Public Shared Function GetApplicationPath() As String
        Dim strText As String = System.Reflection.Assembly.GetExecutingAssembly.Location()
        Return strText.Substring(0, strText.LastIndexOf("\"))
    End Function

  Public Shared Function ReadArgumentValueFile(ByVal strFile As String) As NameValueCollection
    'Read argument-value pairs from a textfile, and return collection with them
        Dim strReadLines() As String = Nothing, NameValues As New System.Collections.Specialized.NameValueCollection()

    Try
      FileOpen(5, strFile, OpenMode.Input, OpenAccess.Read)
    Catch e As Exception
            MsgBox("SystemUtil.ReadArgumentValueFile:" & e.Message, CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle))
      Return Nothing
    End Try

    Console.WriteLine("Reading argument-value file " & strFile)
    Do While Not EOF(5)
      'Read lines and put in array of lines
      Dim strText As String = LineInput(5)

      If strText.Length > 0 Then
        Dim iCurrent As Integer
        If strReadLines Is Nothing Then
          ReDim strReadLines(0)
          iCurrent = 0
        Else
          iCurrent = strReadLines.GetUpperBound(0) + 1
        End If
        ReDim Preserve strReadLines(iCurrent)

        strReadLines(iCurrent) = strText
      End If
    Loop

    'Find the values corresponding with those in the strNames
    Dim i As Integer
    For i = 0 To strReadLines.GetUpperBound(0)
      'Go through each line and store in collection ...

      'Parse name and value
      Dim strItems() As String = Split(Trim(strReadLines(i)), "=")
      If strItems.Length = 2 Then
        'Add argument and value
        NameValues.Add(strItems(0), strItems(1))
      End If
    Next

    'close file
    FileClose(5)

    Return NameValues
  End Function

    Public Overloads Shared Function WriteArgumentValueFile(ByVal strFile As String, ByRef NameValues As NameValueCollection) As Boolean
        'Write argument-value pairs to a textfile, using a collection
        Try
            FileOpen(5, strFile, OpenMode.Output, OpenAccess.Write)
        Catch e As Exception
            MsgBox("WriteArgumentValueFile:" & e.Message, CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle))
            Return False
        End Try

        Console.WriteLine("Write argument-value file " & strFile)

        'Find the values corresponding with those in the strNames
        Dim i As Integer
        For i = 0 To NameValues.Count - 1
            PrintLine(5, NameValues.GetKey(i) & "=" & NameValues.Get(i))
        Next

        'close file
        FileClose(5)
        Return True
    End Function

    Public Overloads Shared Function WriteArgumentValueFile(ByVal strFile As String, _
                                                            ByVal strNames() As String, _
                                                            ByRef strValues() As String) As Boolean
        'Write argument-value pairs to a textfile
        Try
            FileOpen(5, strFile, OpenMode.Output, OpenAccess.Write)
        Catch e As Exception
            MsgBox("WriteArgumentValueFile:" & e.Message, CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle))
            Return False
        End Try

        Console.WriteLine("Write argument-value file " & strFile)

        'Find the values corresponding with those in the strNames
        Dim i As Integer
        For i = 0 To strNames.GetUpperBound(0)
            PrintLine(5, strNames(i) & "=" & strValues(i))
        Next

        'close file
        FileClose(5)

        Return True
    End Function

  Public Shared Function ReadUserKey(ByVal strKeyPath As String, ByVal strName As String) As Object
    Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(strKeyPath)
    If key Is Nothing Then
      Return Nothing
    Else
      Return key.GetValue(strName)
    End If
  End Function

  Public Shared Sub WriteUserKey(ByVal strKeyPath As String, ByVal strName As String, ByVal value As Object)
    Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(strKeyPath, True)
    If key Is Nothing Then
      'Create it
      key = Registry.CurrentUser.CreateSubKey(strKeyPath)
    End If
    key.SetValue(strName, value)
  End Sub

  Public Shared Function GetImageFileType(ByVal strFilename As String) As System.Drawing.Imaging.ImageFormat
    Dim strExt As String = System.IO.Path.GetExtension(strFilename)
    Select Case strExt
      Case ".jpg", ".jpeg"
        Return System.Drawing.Imaging.ImageFormat.Jpeg
      Case ".png"
        Return System.Drawing.Imaging.ImageFormat.Png
      Case ".tif", ".tiff"
        Return System.Drawing.Imaging.ImageFormat.Tiff
      Case ".bmp"
        Return System.Drawing.Imaging.ImageFormat.Bmp
      Case ".gif"
        Return System.Drawing.Imaging.ImageFormat.Gif
        End Select
        Return Nothing
  End Function

End Class
